﻿using MSSClient.Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Net;
using System.Text;
using TestTreeView.Model;

namespace MSSClient
{
    public class OCRService
    {
        public static readonly OCRService Instance = new OCRService();
        private static string ocrserviceUrl = ConfigurationManager.AppSettings["ocrserviceUrl"];
        //访问OCR识别服务接口
        public string visitOCRService(OCRInputParam ocrInputParam)
        {
            string retString = "";
            if (null != ocrInputParam)
            {
                string json = JsonConvert.SerializeObject(ocrInputParam);
                Console.WriteLine(json);
                retString = HttpPost(ocrserviceUrl, json);
            }
            return retString;

        }

        private string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            //request.ContentType = "application/x-www-form-urlencoded";
            request.ContentType = "application/json";
            request.MediaType = "application/json";
            request.Accept = "application/json";
            request.ContentLength = postDataStr.Length;
            StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII);
            writer.Write(postDataStr);
            writer.Flush();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            string encoding = response.ContentEncoding;
            if (encoding == null || encoding.Length < 1)
            {
                encoding = "UTF-8"; //默认编码  
            }
            StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(encoding));
            string retString = reader.ReadToEnd();

            return retString;
        }
    }
}
